﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModelLayer.Patients;
using ModelLayer;

namespace DataAccessLayer.Patients
{
    public class PatientsDataAccess
    {
        static MedAgendaEntities entities = new MedAgendaEntities();

        public static string InsertNewTempPatient(TempPatient patient)
        {
            Patient_ p = new Patient_();
            p.OID_ = Guid.NewGuid();
            p.Version_ = 1;
            entities.Patient_.AddObject(p);

            PatientProfile_ pf = new PatientProfile_();
            pf.OID_ = Guid.NewGuid();
            pf.Version_ = 1;
            pf.VipPatient_ = patient.VipPatient;
            pf.MrnId_ = patient.Pid;
            pf.FamilyName_ = patient.FamilyName;
            pf.GivenName_ = patient.GivenName;
            pf.MiddleName_ = patient.MiddleName;
            pf.DateOfBirth_ = DateTime.Parse(patient.BirthDate);
            pf.DeathIndicator_ = false;
            pf.Patient__OID_ = p.OID_;
            pf.SexEnum__Code_ = patient.Sex;
            pf.IdDocumentNumber_ = patient.IdDocument;
            pf.PatientTypeEnum_Code_ = (from pte in entities.PatientTypeEnum_ where pte.Value_.Equals(patient.PatientType) select pte.Code_).Single();
            pf.SpokenLanguageEnum__Code_ = "es";
            pf.ReligionEnum__Code_ = "VAR";
            pf.InformationAuthorityEnum_Code_ = "X";
            entities.PatientProfile_.AddObject(pf);

            PatientProfileTelephoneNumber_ pt = new PatientProfileTelephoneNumber_();
            pt.PatientProfile__OID_ = pf.OID_;
            pt.Number_ = patient.TelephoneNumber;
            pt.TelephoneUseEnum__Code_ = "PRN";
            pt.TelephoneEquipmentEnum__Code_ = "PH";
            pt.OID_ = Guid.NewGuid();
            entities.PatientProfileTelephoneNumber_.AddObject(pt);

            PatientProfileTelephoneNumber_ pm = new PatientProfileTelephoneNumber_();
            pm.PatientProfile__OID_ = pf.OID_;
            pm.Number_ = patient.MobileNumber;
            pm.TelephoneUseEnum__Code_ = "PRN";
            pm.TelephoneEquipmentEnum__Code_ = "CP";
            pm.OID_ = Guid.NewGuid();
            entities.PatientProfileTelephoneNumber_.AddObject(pm);

            PatientProfileEmailAddress_ pe = new PatientProfileEmailAddress_();
            pe.PatientProfile__OID_ = pf.OID_;
            pe.Address_ = patient.Email;
            pe.OID_ = Guid.NewGuid();
            entities.PatientProfileEmailAddress_.AddObject(pe);

            PatientProfileAddress_ pa = new PatientProfileAddress_();
            pa.PatientProfile__OID_ = pf.OID_;
            pa.Street_ = " ";
            pa.City_ = " ";
            pa.Province_ = " ";
            pa.AddressTypeEnum__Code_ = "R";
            pa.OID_ = Guid.NewGuid();
            entities.PatientProfileAddress_.AddObject(pa);

            PatientProfileContactPerson_ pc = new PatientProfileContactPerson_();
            pc.PatientProfile__OID_ = pf.OID_;
            pc.Name_ = " ";
            pc.ContactPersonTypeEnum__Code_ = "EC";
            pc.ContactPersonRelationshipEnum__Code_ = "OTH";
            pc.OID_ = Guid.NewGuid();
            entities.PatientProfileContactPerson_.AddObject(pc);

            entities.SaveChanges();
            return patient.Pid;
        }

        public static string GetPatientOID(string PID, string familyName, string givenName)
        {
            return entities.PatientProfile_.Where(x => x.MrnId_.Equals(PID)).First().Patient__OID_.ToString();
        }

        public static long GetLastPID()
        {
            //GestorBD.Query = "SELECT MAX(CAST(MrnId_ AS BigInt)) FROM PatientProfile_";
            //return long.Parse(GestorBD.EjecutarSentencia().Rows[0].ItemArray[0].ToString());
            return 0;
        }

        public static string GetPatientsFullNameFromPatientOID(string OID)
        {
            //    GestorBD.Query = "SELECT PatientProfile_.FamilyName_ + ', ' + PatientProfile_.GivenName_ AS FullName" +
            //                    " FROM Patient_ INNER JOIN PatientProfile_ ON Patient_.OID_ = PatientProfile_.PatientOID_" +
            //                    " WHERE (Patient_.OID_ = '" + OID + "')";
            //    return GestorBD.EjecutarSentencia().Rows[0][0].ToString();
            return "";
        }

        public static string GetPatientProfileOIDFromPatientOID(string poid)
        {
            //GestorBD.Query = "SELECT PatientProfile_.OID_ " +
            //                 "FROM Patient_ INNER JOIN PatientProfile_ ON Patient_.OID_ = PatientProfile_.PatientOID_ " +
            //                 "WHERE (Patient_.OID_ = '" + poid + "')";
            //return GestorBD.EjecutarSentencia().Rows[0][0].ToString();
            return "";
        }

        public static TempPatient GetTempPatient(Guid oid)
        {
            TempPatient p = new TempPatient();
            //GestorBD.Query = "SELECT PP.MrnId_, PP.FamilyName_, PP.GivenName_, PP.MiddleName_, PP.DateOfBirth_, PP.Sex_ " +
            //                 " FROM PatientProfile_ AS PP" +
            //                " WHERE PatientOID_ = '" + oid.ToString() + "'";
            //DataTable table = GestorBD.EjecutarSentencia();
            //p.Pid = table.Rows[0][0].ToString();
            //p.FamilyName = table.Rows[0][1].ToString();
            //p.GivenName = table.Rows[0][2].ToString();
            //p.MiddleName = table.Rows[0][3].ToString();
            //p.BirthDate = DateTime.Parse(table.Rows[0][4].ToString()).ToShortDateString();
            //p.Sex = table.Rows[0][5].ToString();
            return p;
        }
    }
}
